﻿using System;
using System.Collections.Generic;
using Cms.Application.Admin.Presenters;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Cms.Domain;
using Cms.Application.Admin.Views;
using Moq;

namespace Cms.Tests.Unit.Application.Admin.Presenters
{
    [TestClass]
    public class EditMembershipPresenterTests : BasePresenterTests
    {
        private Site _site;
        private Administrator _admin;
        private User _user;
        private IList<UserGroup> _groups;
        private Mock<IEditMembershipView> _viewMock;
        private EditMembershipPresenter _presenter;

        private void Prepare()
        {
            _site = new Site { ID = 1 };
            _admin = new Administrator
            {
                DefaultSite = _site,
                DefaultSystemLocale = "en-US",
            };
            InitRepositoryFactoryMock();
            AdministratorRepositoryMock.Setup(a => a.GetAdministrator(1)).Returns(_admin);
            _viewMock = new Mock<IEditMembershipView>();
            _viewMock.SetupAllProperties();
            _viewMock.Setup(v => v.AdministratorID).Returns(1);
            _viewMock.Setup(v => v.Localization).Returns("en-US");
            _viewMock.Setup(v => v.ContextTranslationFile).Returns("Admin/Translations/EditAdministrator.xml");
            _user = new User() { ID = 1 };
            UserRepositoryMock.Setup(m => m.GetUser(1)).Returns(_user);
            UserGroup regular = new UserGroup() { ID = 1, Name = "Regular" };
            UserGroup vip = new UserGroup() { ID = 2, Name = "VIP" };
            _groups = new List<UserGroup> {regular, vip};
            UserRepositoryMock.Setup(m => m.GetUserGroups()).Returns(_groups);
            _viewMock.Setup(v => v.UserID).Returns(1);
            _presenter = new EditMembershipPresenter(_viewMock.Object, FactoryMock.Object);
        }

        [TestMethod]
        public void Initialize_NewMembership_UserGroupsArePopulated()
        {
            Prepare();
            _presenter.Initialize();
            _viewMock.Verify(m => m.SetUserGroups(_groups));
        }

        [TestMethod]
        public void Save_NewMembership()
        {
            Prepare();
            _viewMock.Setup(v => v.StartDate).Returns(new DateTime(2012, 9, 23));
            _viewMock.Setup(v => v.EndDate).Returns(new DateTime?());
            _viewMock.Setup(v => v.SelectedUserGroupID).Returns(2);
            UserRepositoryMock.Setup(m => m.GetUserGroup(2)).Returns(_groups[1]);
            Assert.AreEqual(0, _user.Memberships.Count);
            _presenter.Save();
            Assert.AreEqual(1, _user.Memberships.Count);
            Assert.AreEqual(new DateTime(2012, 9, 23), _user.Memberships[0].StartDate.Value);
            Assert.IsNull(_user.Memberships[0].EndDate);
            Assert.AreEqual(_user, _user.Memberships[0].User);
            Assert.AreEqual("VIP", _user.Memberships[0].UserGroup.Name);
        }
    }
}
